export default{
  namespaced: true,
  state: {
    type:'all',
    list: [{
      id: 100,
      name: "吃饭",
      isDone: true
    },
    {
      id: 201,
      name: "睡觉",
      isDone: false
    },
    {
      id: 103,
      name: "打豆豆",
      isDone: true
    },
  ]
  },
  mutations: {
    changeDoneFn(state,id){
      const obj = state.list.find(item => item.id === id)
      obj.isDone = !obj.isDone
    },
    delFn(state,id){
      return state.list = state.list.filter(item => item.id !== id)
    },
    add(state,content){
      return state.list.unshift({
        id:new Date(),
        name:content,
        isDone:false
      })
    },
    clearFn(state){
      return state.list = state.list.filter(item => !item.isDone)
    },
    changeFn(state,type){
      state.type = type
    },
    changeIsAll(state,val){
      return state.list.forEach(item => item.isDone = val)
    }
  },
    actions:{
      clearFnAsync(context){
        setTimeout(() => {
          context.commit('clearFn')
        }, 1000);
      }
    },
  getters:{
    leftCount(state){
      return state.list.filter(item => !item.isDone).length
    },
    clearShow(state){
      return state.list.some(item => item.isDone)
    },
    showList(state){
      if(state.type === 'active'){
        return state.list.filter(item => !item.isDone)
      }else if(state.type === 'complete'){
        return state.list.filter(item => item.isDone)
      }else{
        return state.list
      }
    }
  }
}